home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Bin / DXUtils / Visual Studio 6.0 Wizards / DMToolWizard.awx / TEMPLATE / CONTROLHELP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-11  |  6.1 KB  |  242 lines

  1. //------------------------------------------------------------------------------
  2. // File: ControlHelp.cpp
  3. //
  4. // Desc: DirectMusicTool DMO Wizard generated code - implementation of CSliderValue and
  5. //       CRadioChoice classes.
  6. //
  7. // Copyright (c) Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif // _MSC_VER > 1000
  13.  
  14. #define STRICT
  15. #ifndef _WIN32_WINNT
  16. #define _WIN32_WINNT 0x0400
  17. #endif
  18.  
  19. #define _ATL_FREE_THREADED
  20. #define _ATL_STATIC_REGISTRY
  21.  
  22. #include <atlbase.h>
  23. //You may derive a class from CComModule and use it if you want to override
  24. //something, but do not change the name of _Module
  25. extern CComModule _Module;
  26. #include <atlcom.h>
  27. #include <atlctl.h>
  28. #include <statreg.h>
  29. #include <statreg.cpp>
  30. #include <atlimpl.cpp>
  31.  
  32. #include "ControlHelp.h"
  33. #include <commctrl.h>
  34. #include <stdio.h>
  35.  
  36. //////////////////////////////////////////////////////////////////////////////
  37. // CSliderValue
  38.  
  39. const short g_sMaxContinuousTicks = 100;
  40. const int g_iMaxCharBuffer = 50; // # characters big enough to hold -FLT_MAX with room to spare
  41.  
  42. CSliderValue::CSliderValue()
  43.   : m_fInit(false)
  44. {
  45. }
  46.  
  47. void CSliderValue::Init(
  48.         HWND        hwndSlider,
  49.         HWND        hwndEdit,
  50.         float       fMin, 
  51.         float       fMax, 
  52.         bool        fDiscrete)
  53. {
  54.     m_hwndSlider = hwndSlider;
  55.     m_hwndEdit = hwndEdit;
  56.     m_fMin = fMin;
  57.     m_fMax = fMax;
  58.     m_fDiscrete = fDiscrete;
  59.  
  60.     short sMin;
  61.     short sMax;
  62.     short sTicks = 4; // Lots of ticks become less useful as guides.  Use quarters for fine-grained sliders.
  63.     if (m_fDiscrete) 
  64.     {
  65.         sMin = static_cast<short>(fMin);
  66.         sMax = static_cast<short>(fMax);
  67.         if (sMax - sMin <= 10)
  68.             sTicks = (short) (sMax - sMin);
  69.     }
  70.     else
  71.     {
  72.         sMin = 0;
  73.         sMax = g_sMaxContinuousTicks;
  74.     }
  75.     
  76.     SendMessage(m_hwndSlider, TBM_SETRANGE, TRUE, MAKELONG(sMin, sMax));
  77.     SendMessage(m_hwndSlider, TBM_SETTICFREQ, (sMax - sMin) / sTicks, 0);
  78.     m_fInit = true;
  79. }
  80.  
  81. void CSliderValue::SetValue(float fPos)
  82. {
  83.     if (!m_fInit)
  84.         return;
  85.  
  86.     UpdateEditBox(fPos);
  87.     UpdateSlider();
  88. }
  89.  
  90. float CSliderValue::GetValue()
  91. {
  92.     if (!m_fInit)
  93.         return 0;
  94.  
  95.     LRESULT lrLen = SendMessage(m_hwndEdit, WM_GETTEXTLENGTH, 0, 0);
  96.     if (lrLen >= g_iMaxCharBuffer)
  97.         return 0;
  98.  
  99.     char szText[g_iMaxCharBuffer] = "";
  100.     SendMessage(m_hwndEdit, WM_GETTEXT, g_iMaxCharBuffer, reinterpret_cast<LPARAM>(szText));
  101.  
  102.     float fVal = static_cast<float>(m_fDiscrete ? atoi(szText) : atof(szText));
  103.  
  104.     if (fVal < m_fMin) fVal = m_fMin;
  105.     if (fVal > m_fMax) fVal = m_fMax;
  106.     return fVal;
  107. }
  108.  
  109. float CSliderValue::GetSliderValue()
  110. {
  111.     short sPos = static_cast<short>(SendMessage(m_hwndSlider, TBM_GETPOS, 0, 0));
  112.     if (m_fDiscrete)
  113.     {
  114.         return sPos;
  115.     }
  116.  
  117.     float fRet = (m_fMax - m_fMin) * sPos / g_sMaxContinuousTicks + m_fMin;
  118.     return fRet;
  119. }
  120.  
  121. LRESULT CSliderValue::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  122. {
  123.     if (!m_fInit)
  124.         return FALSE;
  125.  
  126.     bHandled = FALSE;
  127.  
  128.     switch (uMsg)
  129.     {
  130.     case WM_HSCROLL:
  131.         if (reinterpret_cast<HWND>(lParam) == m_hwndSlider && LOWORD(wParam) >= TB_LINEUP && LOWORD(wParam) <= TB_ENDTRACK)
  132.         {
  133.             UpdateEditBox(GetSliderValue());
  134.             bHandled = TRUE;
  135.         }
  136.         break;
  137.  
  138.     case WM_COMMAND:
  139.         if (HIWORD(wParam) == EN_KILLFOCUS && reinterpret_cast<HWND>(lParam) == m_hwndEdit)
  140.         {
  141.             UpdateSlider();
  142.             bHandled = TRUE;
  143.         }
  144.         break;
  145.     }
  146.  
  147.     return 0;
  148. }
  149.  
  150. void CSliderValue::UpdateEditBox(float fPos)
  151. {
  152.     char szText[g_iMaxCharBuffer] = "";
  153.  
  154.     if (m_fDiscrete)
  155.     {
  156.         short sPos = static_cast<short>(fPos);
  157.         sprintf(szText, "%hd", sPos);
  158.     }
  159.     else
  160.     {
  161.         sprintf(szText, "%.3hf", fPos);
  162.     }
  163.  
  164.     SendMessage(m_hwndEdit, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(szText));
  165. }
  166.  
  167. void CSliderValue::UpdateSlider()
  168. {
  169.     float fVal = GetValue();
  170.     short sPos = static_cast<short>(m_fDiscrete ? fVal : g_sMaxContinuousTicks * ((fVal - m_fMin) / (m_fMax - m_fMin)));
  171.     SendMessage(m_hwndSlider, TBM_SETPOS, TRUE, sPos);
  172.     UpdateEditBox(fVal); // this resets the input box back to the set float value in case the input was invalid
  173. }
  174.  
  175. //////////////////////////////////////////////////////////////////////////////
  176. // CRadioChoice
  177.  
  178. CRadioChoice::CRadioChoice(const ButtonEntry *pButtonInfo)
  179.   : m_pButtonInfo(pButtonInfo)
  180. {
  181. }
  182.  
  183. void CRadioChoice::SetChoice(HWND hDlg, LONG lValue)
  184. {
  185.     for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  186.     {
  187.         if (p->lValue == lValue)
  188.         {
  189.             CheckDlgButton(hDlg, p->nIDDlgItem, BST_CHECKED);
  190.             return;
  191.         }
  192.     }
  193. }
  194.  
  195. LONG CRadioChoice::GetChoice(HWND hDlg)
  196. {
  197.     for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  198.     {
  199.         if (BST_CHECKED == IsDlgButtonChecked(hDlg, p->nIDDlgItem))
  200.         {
  201.             return p->lValue;
  202.         }
  203.     }
  204.  
  205.     return 0;
  206. }
  207.  
  208. LRESULT CRadioChoice::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  209. {
  210.     bHandled = FALSE;
  211.     UNREFERENCED_PARAMETER(lParam);
  212.  
  213.     if (uMsg == WM_COMMAND && HIWORD(wParam) == BN_CLICKED)
  214.     {
  215.         for (const ButtonEntry *p = m_pButtonInfo; p->nIDDlgItem; ++p)
  216.         {
  217.             if (p->nIDDlgItem == LOWORD(wParam))
  218.             {
  219.                 bHandled = TRUE;
  220.                 return 0;
  221.             }
  222.         }
  223.     }
  224.  
  225.     return 0;
  226. }
  227.  
  228. //////////////////////////////////////////////////////////////////////////////
  229. // MessageHandlerChain
  230.  
  231. LRESULT MessageHandlerChain(Handler **ppHandlers, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  232. {
  233.     LRESULT lr = 0;
  234.     bHandled = FALSE;
  235.  
  236.     for (Handler **pp = ppHandlers; *pp && !bHandled; ++pp)
  237.     {
  238.         lr = (*pp)->MessageHandler(uMsg, wParam, lParam, bHandled);
  239.     }
  240.     return lr;
  241. }
  242.